package com.mjw.tender.feign.fallback;

import com.mjw.tender.domain.TbUser;
import com.mjw.tender.dto.ResponseResult;
import com.mjw.tender.feign.Oauth2Feign;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class Oauth2FeignFallBackFactory implements FallbackFactory<Oauth2Feign> {
    @Override
    public Oauth2Feign create(Throwable throwable) {
        log.error(throwable.getMessage());
        return new Oauth2FeignFallBack() {
            @Override
            public ResponseResult getUserInfo() {
                if(throwable.getMessage()!=null && throwable.getMessage().contains("status 401")){
                    return new ResponseResult(ResponseResult.CodeStatus.ILLEGAL_TOKEN,"拉取用户信息失败");
                }
                return new ResponseResult(ResponseResult.CodeStatus.FAIL,"未知异常");
            }

            @Override
            public ResponseResult<Long> addUserReturnId(TbUser tbUser) {
                return new ResponseResult(ResponseResult.CodeStatus.FAIL,"添加用户失败");
            }
        };
    }
}
